#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <unordered_set>
#include <unordered_map>
#include <functional>
#include <cmath>
#include <cstring>

using namespace std;

class LRUCache
{
    class ListNode
    {
    public:
        int key;
        int value;
        ListNode *nxt;
        ListNode *pre;
        ListNode(int key = 0, int value = 0) : key(key), value(value), nxt(nullptr), pre(nullptr) {}
    };
    ListNode mHost;
    unordered_map<int, ListNode *> mHashMaps;

    ListNode *remove(ListNode *node)
    {
        node->pre->nxt = node->nxt;
        node->nxt->pre = node->pre;
        node->pre = node->nxt = nullptr;
        return node;
    }

    void insert(ListNode *node)
    {
        mHost.pre->nxt = node;
        node->pre = mHost.pre;
        mHost.pre = node;
        node->nxt = &mHost;
    }

    int getValue(ListNode *node)
    {
        remove(node);
        insert(node);
        return node->value;
    }

    void setValue(ListNode *node, int newValue)
    {
        remove(node);
        insert(node);
        node->value = newValue;
    }

public:
    LRUCache(int capacity)
    {
        mHost.nxt = &mHost;
        mHost.pre = &mHost;
        mHost.value = capacity;
    }

    int get(int key)
    {
        if (!mHashMaps.count(key))
        {
            return -1;
        }
        ListNode *node = mHashMaps[key];
        return getValue(node);
    }

    void put(int key, int value)
    {
        if (mHashMaps.count(key))
        {
            setValue(mHashMaps[key], value);
            return;
        }
        if (mHashMaps.size() >= mHost.value)
        {
            ListNode *eraseNode = remove(mHost.nxt);
            mHashMaps.erase(eraseNode->key);
            delete (eraseNode);
        }
        ListNode *node = new ListNode(key, value);
        insert(node);
        mHashMaps[key] = node;
    }
};

/**
 * Your LRUCache object will be instantiated and called as such:
 * LRUCache* obj = new LRUCache(capacity);
 * int param_1 = obj->get(key);
 * obj->put(key,value);
 */

int main()
{
    // system("pause");
    return 0;
}